﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using PeekService;
using System.Web.SessionState;

namespace PeekWebApp.Management.AjaxHandler
{
    /// <summary>
    /// adminHandler 的摘要说明
    /// </summary>
    public class adminHandler : IHttpHandler, IRequiresSessionState 
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string username = context.Server.HtmlDecode(context.Request["username"].Trim());
            string password = context.Server.HtmlDecode(context.Request["password"].Trim());
            SysUser user = new SysUserService().GetAll().Find(o => (o.UserName.Equals(username) && o.PassWord.Equals(password)&&o.SysRole.Equals("管理员"))
                );
            if (user != null)
            {
                context.Response.Cookies.Add(new HttpCookie("UserAdmin", DecryptService.Encrypt(user.Code)));
              //  context.Session.Add("User", user);
                try
                {

                    context.Response.Write(context.Request.UrlReferrer.AbsoluteUri.Replace("admin.aspx", "Management/manage.aspx"));
                }
                catch
                {
                    context.Response.Write(context.Request.RawUrl.ToString());//返回当前请求的地址
                }

            }
            else
            {
                context.Response.Write("0");
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}